﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TVGuide.DataAccess.Common;
using TVGuide.DataAccess.Repository;
using TVGuide.Common;
using System.Globalization;
using System.Text;
using System.Web.Routing;

namespace TVGuide.UI.HTMLHelpers
{
    public static class CustomHTMLHelpers
    {
        private static CommonStr comm_str = new CommonStr(ResUtil.GetLang(1));
        /*We expect here a programList which contains program objects with same name property*/
        public  static MvcHtmlString AcordionElement (this HtmlHelper helper,
                                                     List<Program> programList)
        {
            if (programList.Count==0)
            {
                return MvcHtmlString.Create("");
            }
            System.Text.StringBuilder StrHtml = new System.Text.StringBuilder();

            var channelRep = ChannelRepository.Instance();
            var channel = channelRep.Get(programList.ElementAt(0).ChannelId);
            StrHtml.Append("<h3><a href='#'><div class='acc-short-title'>" + (programList.ElementAt(0).Name.Length > 87 
                ? programList.ElementAt(0).Name.Substring(0, 87) + "..."
                : programList.ElementAt(0).Name) + "</div><img  class=\"search-channelImage\"alt=\"" + channel.DisplayName
                + "\" src=\"" + channel.ImageURL + "\" title=\"" + channel.DisplayName + "\" />" + "</a></h3>");
            StrHtml.Append("<div>");


            var dates = programList.Select(o => o.Date.Day).Distinct().ToList();

            foreach (var date in dates)
            {
                var progsForCurrentDay = programList.Select(o => o).Where(a => a.Date.Day == date).ToList();
                StrHtml.Append(formatForDay(progsForCurrentDay));
            }


            StrHtml.Append("</div>");

            return MvcHtmlString.Create(StrHtml.ToString());
        }

        private static string formatForDay(List<Program> progsForCurrentDay)
        {
            if (progsForCurrentDay==null || progsForCurrentDay.Count==0)
            {
                return "";
            }
            System.Text.StringBuilder StrHtml = new System.Text.StringBuilder();
            var progDayOfWeek = (int)progsForCurrentDay.First().Date.DayOfWeek;
            var progDate = progsForCurrentDay.First().Date;
            var day = "";
            switch (progDayOfWeek)
            {
                case 1:
                    day = comm_str.MONDAY_LBL;
                    break;
                case 2:
                    day = comm_str.TUESDAY_LBL;
                    break;
                case 3:
                    day = comm_str.WEDNESDAY_LBL;
                    break;
                case 4:
                    day = comm_str.THURSDAY_LBL;
                    break;
                case 5:
                    day = comm_str.FRIDAY_LBL;
                    break;
                case 6:
                    day = comm_str.SATURDAY_LBL;
                    break;
                case 0:
                    day = comm_str.SUNDAY_LBL;
                    break;
            }

            string statusClass = "";
            if (DateTime.Today > progDate)
            {
                statusClass = "passed";
            }
            else
            {
                statusClass = "comming";
            }
            string aysor = "";
            if (progDate.Day == DateTime.Today.Day)
            {
                aysor = " (այսօր)";
            }
            
            StrHtml.Append("<div class=\"progs-for-day\"><span class=\"acardion-searchResults-span "+statusClass+"\">");
            StrHtml.Append("<div class=\"day-title\"><p><b>"+day+aysor+"</b></p><p>" + progDate.ToString("dd/MM/yyyy") + "</p></div>");
            
            DateTimeFormatInfo myDTFI = new CultureInfo( "hy-AM", false ).DateTimeFormat;
            int i = 0;
            foreach (var item in progsForCurrentDay)
            {
                StrHtml.Append("<p class=\"" + (i++ % 2 == 0 ? "prog-time-even" : "prog-time-odd") + "\">" + item.Date.ToString(myDTFI.ShortTimePattern) + "</br></p>");
            }
            StrHtml.Append("</span></div>");
            return StrHtml.ToString();
        }


        public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo)
        {
            return htmlHelper.CheckBoxList(name, listInfo, ((IDictionary<string, object>)null));
        }

        public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo, object htmlAttributes)
        {
            return htmlHelper.CheckBoxList(name, listInfo, ((IDictionary<string, object>)new RouteValueDictionary(htmlAttributes)));
        }

        public static string CheckBoxList(this HtmlHelper htmlHelper, string name, List<CheckBoxListInfo> listInfo, IDictionary<string, object> htmlAttributes)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentException("The argument must have a value", "name");
            if (listInfo == null)
                throw new ArgumentNullException("listInfo");
            if (listInfo.Count < 1)
                throw new ArgumentException("The list must contain at least one value", "listInfo");
            StringBuilder sb = new StringBuilder();
            foreach (CheckBoxListInfo info in listInfo)
            {
                TagBuilder builder = new TagBuilder("input");
                if (info.IsChecked) builder.MergeAttribute("checked", "checked");
                builder.MergeAttributes<string, object>(htmlAttributes);
                builder.MergeAttribute("type", "checkbox");
                builder.MergeAttribute("value", info.Value);
                builder.MergeAttribute("name", name);
                builder.InnerHtml = info.DisplayText;
                sb.Append(builder.ToString(TagRenderMode.Normal));
                sb.Append("<br />");
            }
            return sb.ToString();
        }

    }

    public class CheckBoxListInfo
    {
        public CheckBoxListInfo(string value, string displayText, bool isChecked)
        {
            this.Value = value;
            this.DisplayText = displayText;
            this.IsChecked = isChecked;
        }
        public string Value { get; private set; }
        public string DisplayText { get; private set; }
        public bool IsChecked { get; private set; }
    }
    
}